Skip to content

fix(runtime): convert Java char values as UTF-16 code units - #2140

Merged
edusperoni merged 3 commits into
mainfrom
fix/char-conversion-and-field-handle
Nov 21, 2026
Merged

edusperoni merged 3 commits into
mainfrom
fix/char-conversion-and-field-handle

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Fixes Java char conversion for non-ASCII values, and escapes one un-escaped handle in FieldAccessor::GetJavaField.

Bug 1: char values went through UTF-8

Several JS→Java char conversions turned the JS string into UTF-8 and kept a single byte, so any non-ASCII value reached Java wrong. Observed on main with the new specs:

Path Input Java got (before)
char("é") / char("€") method argument (JsArgConverter, CastType::Char) U+00E9 / U+20AC 195 / 226
char field set, instance and static (FieldAccessor::SetJavaField) U+00E9 / U+20AC 195 / 226
char() returned from a JS callback (JsArgToArrayConverter, CastType::Char) U+00E9 / U+20AC sent in via char() 195 / 195 (the callback already received à / â from the argument bug, and the returned char() again kept the first UTF-8 byte)
JS array → char[] argument (JsArgConverter::ConvertJavaScriptArray) ["é", "€", "中", "\ud83d"] 234, 232, 235, 235
char[] element set (ArrayElementAccessor::SetArrayElement) same 234, 232, 235, 235
char[] element get (ArrayElementAccessor::GetArrayElement) Java 'é', '€' JS got U+FFFD
Plain JS string to a non-overloaded char parameter (JsArgConverter::ConvertJavaScriptString) "x", "é", "" 4141, 4133, 4137 (the low bits of the jstring local reference, read through jvalue.c)

The array paths reinterpreted the first two UTF-8 bytes as one UTF-16 unit and then took a byte of its modified UTF-8. The element get handed V8 one UTF-8 byte.

Fix. All of these now go through a new ArgConverter::ToJavaChar, which takes the first UTF-16 code unit of the JS string. An empty string gives U+0000, which is what the field setter already gave. A character outside the BMP gives its high surrogate. The char[] element get uses jcharToV8String. No new errors are thrown.

Checked and already correct, so unchanged:

  • char method return values
  • char field get
  • char arguments of Java→JS callbacks
  • a raw one-character string returned from a callback for a char return type (TryGetPrimitiveReturnBits)
  • Uint16Array → char[]

Behaviour change to review. A plain string passed to a non-overloaded char parameter now yields its first code unit, where it used to pass an arbitrary value. Overloaded methods are unaffected because overload resolution never matches a String to char. If a "Cannot convert string to C" error is preferred instead, that is a small change in ConvertJavaScriptString.

Bug 2: boolean field read returned an un-escaped handle

GetJavaField escapes every result from its EscapableHandleScope except the boolean branch, which returned a handle created inside the closed scope. V8's Boolean::New currently returns a root-table slot (v8-primitive.h: True() / False() use GetRootSlot), so this does not dangle in practice. It is now escaped like the sibling branches, because the API does not guarantee root slots. The boolean field spec passes before and after, so it is coverage rather than a regression test.

Does your commit message include the wording below to reference a specific issue in this repo?

No linked issue; found by a code audit.

Related Pull Requests

None.

Does your pull request have unit tests?

Yes. There is a new describe in testPrimitiveTypeConversion.js with Java helpers added to com.tns.tests.CharConversionTest.

What the specs cover

  • Inputs: U+00E9, U+20AC, U+4E2D, a lone high surrogate, an ASCII control and ASCII.
  • Paths: char() and plain-string arguments, char return values, JS array → char[], char[] element get and set, instance and static char fields, and a char() returned from a JS callback.
  • Java-side values are read back as ints, and the element get uses a Java-created char[], so a broken direction cannot be masked by the opposite one.
  • They pin the empty-string (U+0000) and multi-unit (first code unit) behaviour.
  • They read instance and static boolean fields.

Results (full suite, runtestsAndVerifyResults, arm64 emulator):

  • Unfixed main with the new specs: 9 new specs failed (41 expectations), and nothing else failed. The plain-string path was probed separately; the values are in the table above.
  • With the fix: 1785 specs, 0 failures, 6 pending.

Summary by CodeRabbit

  • Bug Fixes
    • Java char values now convert consistently using UTF-16 code units, including non-ASCII characters and surrogate pairs. Empty strings map to NUL, and longer strings use their first code unit.
    • Improved character conversion across method calls, arrays, fields, and callbacks.
    • Fixed reading boolean fields from Java objects.

Several JS-to-Java char conversions went through UTF-8 and kept a single
byte, so a non-ASCII value reached Java wrong: char("é") as a method
argument arrived as 195 and char("€") as 226, the same happened for char
fields and for char() values returned from JS callbacks, and a JS array
converted to char[] or a char[] element set reinterpreted the first two
UTF-8 bytes as one UTF-16 unit before taking a byte of it ("é" became
234, "€" 232). Reading a char[] element handed V8 one UTF-8 byte and
produced U+FFFD.

A plain JS string passed to a non-overloaded char parameter was
converted to a java.lang.String whose local reference was then read
through the jvalue's char member, so even "x" arrived as an arbitrary
value.

These paths now share ArgConverter::ToJavaChar, which takes the first
UTF-16 code unit of the JS string (U+0000 for an empty string, the high
surrogate for a character outside the BMP), and the char[] element read
uses jcharToV8String like the method-return, field-get and callback
argument paths already did. Affected: char() cast arguments, plain
string arguments to char parameters, JS array to char[], char[] element
get and set, char field set, and char() values returned from JS
callbacks.
GetJavaField escapes every result from its EscapableHandleScope except
the boolean one, which it returned as a handle created inside the
closed scope. V8's Boolean::New returns a root-table slot today, so the
handle stays valid in practice, but the API does not promise that;
escape it like the other branches.
…d reads

Specs pass U+00E9, U+20AC, U+4E2D, a lone high surrogate, an ASCII
control and ASCII through every char path: char() and plain string
arguments, char return values, JS array to char[], char[] element get
and set, instance and static char fields, and a char() value returned
from a JS callback. Java-side values are read back as ints and the
element read uses a Java-created char[], so a broken direction cannot
be masked by the opposite one. They also pin the empty-string (U+0000)
and multi-unit (first code unit) behaviour, and read instance and static
boolean fields.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The runtime now converts Java char values using UTF-16 code units across argument, array, and field access. Java test helpers and JavaScript tests cover scalar values, arrays, fields, callbacks, and boolean field access.

Changes: Java char conversion

Layer / File(s) Summary
String-to-char argument conversion
test-app/runtime/src/main/cpp/ArgConverter.h, test-app/runtime/src/main/cpp/JsArgConverter.cpp, test-app/runtime/src/main/cpp/JsArgToArrayConverter.cpp
ToJavaChar returns zero for an empty string or the first UTF-16 code unit otherwise. Argument conversion uses it for char casts, scalar strings, and char arrays.
Array and field access
test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp, test-app/runtime/src/main/cpp/FieldAccessor.cpp
Character array and field access use UTF-16 code units. Boolean field reads escape the converted value into the V8 handle scope.
Java test bridge and conversion tests
test-app/app/src/main/java/com/tns/tests/CharConversionTest.java, test-app/app/src/main/assets/app/tests/testPrimitiveTypeConversion.js
Java helpers expose char values, arrays, fields, and callbacks. JavaScript tests cover UTF-16 code units, empty and longer strings, boxed strings, and boolean fields.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: darind

Merge Risk: Minimal · up to a13cd

This change fixes JavaScript-to-Java char conversion so it uses UTF-16 code units and escapes the boolean field-read result. It adds tests for these cases. No actionable merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: Java char values are now converted as UTF-16 code units instead of through UTF-8. The changed files (ArgConverter.h ToJavaChar, JsArgConverter, JsArgToArrayConverter, …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Java char values are now converted as UTF-16 code units instead of through UTF-8. The changed files (ArgConverter.h ToJavaChar, JsArgConverter, JsArgToArrayConverter, ArrayElementAccessor, FieldAccessor) and the objectives all match this. The title also uses the conventional fix(runtime) prefix. It does not mention the GetJavaField boolean escape fix or the new tests, which is expected for a single-sentence title.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b96e522 and a13cd83.

Files selected for processing: 7

  • test-app/app/src/main/assets/app/tests/testPrimitiveTypeConversion.js
  • test-app/app/src/main/java/com/tns/tests/CharConversionTest.java
  • test-app/runtime/src/main/cpp/ArgConverter.h
  • test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp
  • test-app/runtime/src/main/cpp/FieldAccessor.cpp
  • test-app/runtime/src/main/cpp/JsArgConverter.cpp
  • test-app/runtime/src/main/cpp/JsArgToArrayConverter.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edusperoni
edusperoni marked this pull request as ready for review November 21, 2026 18:41
@edusperoni
edusperoni merged commit 48d1fb1 into main Nov 21, 2026
8 checks passed
@edusperoni
edusperoni deleted the fix/char-conversion-and-field-handle branch November 21, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant